"use strict";
/** 
 * webpack基础配置
*/

const path   = require("path");
const config = require("./config.js");
const utils  = require("./utils");


// console.log("process.env.npm_config_NODEENV:", process.env.npm_config_NODEENV);

module.exports = {
    entry: {
        app: resolve("clientSrc/main.js")
    },
    output: {
        path: config.build.assetsRoot,
        filename: "[name].js",
        chunkFilename: "[name].[hash:7].chunk.js",
        // 配置CDN路径
        publicPath: process.env.npm_config_NODEENV === "production"
            ? config.build.assetsPublicPath
            : config.dev.assetsPublicPath
    },
    // 配置寻找模块的规则
    resolve: {
        extensions: [".js", ".jsx", ".ts", ".tsx"],
        // 配置别名 让你在开发的时候更舒服
        alias: {
            "@src": resolve("clientSrc/"),   
            "@router": resolve("clientSrc/routers"),
            "@components": resolve("clientSrc/components")
        }
    },
    // 配置处理模块的规则
    module: {
        rules: [{
            test: /\.(js|jsx|ts|tsx)$/,                                     
            // loader: "babel-loader", use: ["babel-loader?cacheDirectory=true"]
            use: ["happypack/loader?id=babel&cacheDirectory"],
            include: [resolve("clientSrc")]
        },{
            test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,                          // 匹配图片文件
            loader: "url-loader",
            options: {
                limit: 1024,                                                  // limit: 1024,限制 图片大小 1kB，小于限制会将图片转换为 base64格式
                name: utils.assetsPath("img/[name].[hash:7].[ext]")           // 图片名称进行hash处理
            }
        },
        {
            test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,              // 匹配音频文件
            loader: "url-loader",
            options: {
                limit: 1024,
                name: utils.assetsPath("media/[name].[hash:7].[ext]")
            }
        },
        {
            test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,                         // 匹配字体文件
            loader: "url-loader",
            options: {
                limit: 1024,
                name: utils.assetsPath("fonts/[name].[hash:7].[ext]")
            }
        }]
    },
    // 用于从配置中解析入口起点
    context: path.resolve(__dirname, "../"),

    // node自带的模块，不需要安装，但有时候在实际项目中引入会报错，所以在此设置
    node: {
        setImmediate: false,
        dgram: "empty",
        fs: "empty",
        net: "empty",
        tls: "empty",
        child_process: "empty"
    }
};


// 路径获取的封装
function resolve(dir) {
    return path.join(__dirname, "..", dir);
};
